#include "tree.h"

#define max(a,b) (a>=b?a:b)

struct tree * create_int(int op, int val)
{
  struct tree *t = (struct tree *)malloc(sizeof(struct tree));
  t->l = NULL;
  t->r = NULL;
  t->op = op;
  t->node_val = val;
  return t;
}

struct tree * create_id(int op, char* name)
{
  struct tree *t = (struct tree *)malloc(sizeof(struct tree));
  t->l = NULL;
  t->r = NULL;
  t->op = op;
  t->node_name = name;
  return t;
}

struct tree * build_tree(int op, struct tree *l, struct tree *r)
{
  struct tree *t = (struct tree *)malloc(sizeof(struct tree));
  t->l = l;
  t->r = r;
  t->op = op;
  return t;
}

void display_tree(struct tree *t, int cpt)
{
  printf("Etage : %d, hauteur : %d, op : %d, val : %d\n", 
	 cpt, t->height, t->op, t->node_val);
  if(t->l != NULL)
    display_tree(t->l, cpt+1);
  if(t->r != NULL)
    display_tree(t->r, cpt+1);
  return;
}

int compute_height(struct tree * t)
{
  if(t->r == NULL)
    {
      if(t->l == NULL)
	  return t->height = 1;
      else
	  return t->height = 1 + compute_height(t->l);
    }
  else
    return t->height = 1 + max(compute_height(t->l), compute_height(t->r));
}

/* Balance the tree to left -- A supprimer */
/*int balance(struct tree * t)
{
  int leftDepth, rightDepth;
  if( t->op != ENTIER && t->op != ID )
    {
      if( (leftDepth = balance(t->l)) < (rightDepth = balance(t->r)) )
	{
	  struct tree *tmp = t->l;
	  t->l = t->r;
	  t->r = tmp;
	  //free(tmp);
	}
      printf("leftDepth : %d, rightDepth : %d\n", leftDepth, rightDepth);
      return 1 + leftDepth;
    }
  else
    return 1;
    }*/

 //FONCTION EFFACER ARBRE
